//
// Created by bill3000 on 3/4/21.
//

#include <unistd.h>

#include "fruit.hpp"
#include "../log/logger.hpp"

int FruitFSM::learning_start_sugar_ann_thread(int catalog_num) {
    int ret;
    pthread_t id;
    pthread_attr_t thread_attr;

    ret = pthread_attr_init(&thread_attr);
    std::string ss = "Failed to create suagr ann thread !";
    if (ret != 0) {
        LOG(ERROR, ss);
        return -1;
    }
    ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
    if (ret != 0) {
        LOG(ERROR, ss);
        return -1;
    }
    ret = pthread_create(&id, &thread_attr, learning_sugar_ann_thread, (void *)&catalog_num);
    if(ret!=0) {
        LOG(ERROR, ss);
        return -1;
    }
    pthread_attr_destroy(&thread_attr);

    return 0;
}

void * FruitFSM::learning_sugar_ann_thread(void *) {
    int begin = 0, end = 0;
    cv::Mat m4;

    action_start_conveyor(nullptr);
    util_delete_dir_content("/intelli/tracking/sugar/");

    while(learning) {
        usleep(50000); //delay 50ms

        *cam4 >> m4;
        if (m4.empty()) {
            LOG(ERROR, "Capture image error !");
            learning = false;
            break;
        }
        int area_threshold = m4.rows * m4.cols / 9;

        int raw_area = algorithm_get_image_area(m4);
        int area = 0;
        if (raw_area > area_threshold)
            area = raw_area;

        if (area == 0) {
            if (begin == 0) {  //a new round of start
                begin = 1;
                continue;
            }
            if (end == 1) {
                //Here, we got a perfect series of a fruit images
                //and we must get the one in the middle of the series
                //and copy it to /intelli/working/ subdirectory
                learning_sugar_ann_get_perfect_image_from_series();

                begin = 0;  //start a new round
                end = 0;
            }
            continue;
        }
        if (begin == 0) continue;

        //We get a fruit image and put it in the "/intelli/tracking/sugar/" directory
        end = 1;
        std::string fn = util_get_now_time_milli_string();
        fn.insert(0, "/intelli/tracking/sugar/").append(".jpg");
        cv::imwrite(fn, m4);
    }
    util_delete_dir_content("/intelli/tracking/sugar/");
    util_delete_dir_content("/intelli/working/");
    action_stop_conveyor(nullptr);

    pthread_exit(nullptr);
}

void FruitFSM::learning_sugar_ann_get_perfect_image_from_series() {
    std::vector<std::string> fns;
    std::string sdir = "/intelli/tracking/sugar/";
    util_get_dir_content(sdir, fns);

    unsigned long mid = fns.size() / 2;
    std::string fn = fns.at(mid);

    std::string ddir = "/intelli/working/";
    if (util_copy_file(sdir, fn, ddir) < 0) {
        LOG(ERROR, "Copy file error, Can not annotation anymore !");
        learning = false;
        pthread_exit(nullptr);
    }
    util_delete_dir_content(sdir.c_str());
}

int FruitFSM::learning_start_sugar_learning_thread() {
    int ret;
    pthread_attr_t thread_attr;

    ret = pthread_attr_init(&thread_attr);
    std::string ss = "Failed to create sugar learning thread !";
    if (ret != 0) {
        LOG(ERROR, ss);
        return -1;
    }
    ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
    if (ret != 0) {
        LOG(ERROR, ss);
        return -1;
    }
    ret = pthread_create(&sugar_learning_thread_id, &thread_attr, learning_sugar_learning_thread, nullptr);
    if(ret!=0) {
        LOG(ERROR, ss);
        return -1;
    }
    pthread_attr_destroy(&thread_attr);

    return 0;
}

void* FruitFSM::learning_sugar_learning_thread(void *data) {
    //The needed annotation data under "/intelli/learning/sugar/"
    //label file: mapping.csv
    //and sugar image files
    sugar_learning_progress_in_percent = 0;
    //...
    return nullptr;
}

